<template>
    <view v-if="ifAuthorize" style="background: rgba(0,0,0,0.3);position: fixed;top: 0;width: 100%;height: 100%;">
        <view style="width: 300px;margin: auto;margin-top: 130px;background: #fff;border-radius: 10px;">
            <view class="header1">
                需要您的授权
            </view>
            <view style="text-align: center;">
                <view style="text-align: center;padding-top: 17px;">
                    <view>为了提供更好的服务</view>
                    <view>请允许获取您的用户信息</view>
                    <view>如暂不需要，可点击取消或拒绝</view>
                </view>
              <!--  <view style="text-align: center;">
                    <image src="../static/image/shouquan.png" mode="widthFix" style="width: 230px;"></image>
                </view> -->
                <view style="padding-bottom: 5px;">
                    <button type="default" class="btn" @click="authorize()">授权</button>
                    <button type="default" class="btn" @click="cancel()">取消</button>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
	let App = getApp();
	  export default {
	        name:"authorize",
	        data() {
	            return {
					userData:{},
	                ifAuthorize:true,
					// code:'',
					isCanUse:uni.getStorageSync('isCanUse')||true
	            };
	        },
			async onShow(){
				
			},
		
			methods:{
	            cancel() {
	                this.ifAuthorize = false
	            },
	            async authorize() {
					let that = this
					//获取用户信息
					uni.getUserProfile({
						desc:"登录",
						success(res) {
							that.userData = res.userInfo
							try{
								that.updataUserinfo()
							}catch(e){}
						}
					})

	            },
				async updataUserinfo(){
					let that = this
					uni.showLoading({
						title:"登录中..."
					})
					//新增用户
					const userinfo1 = await that.GQL({
						"operationName": "createSlUser",
						"query": "mutation createSlUser($input: CreateSlUserInput!) { createSlUser(input: $input) { slUser { avatarUrl nickname openid phone level language gender uid } } }",
						"variables": {
							"input": {
								"slUser": {
									"nickname": that.userData.nickName,
									"avatarUrl": that.userData.avatarUrl,
									"openid": uni.getStorageSync('login').openid,
									"phone": uni.getStorageSync("phoneNumber")
								}
							}
						}
					})
					// console.log('11---------',userinfo1.data.createSlUser.slUser)
					if(userinfo1.data.createSlUser.slUser){
						uni.setStorageSync('userData', userinfo1.data.createSlUser.slUser);
						uni.setStorageSync('userinfo', true);
						// console.log('2222-----',uni.getStorageSync("userData"))
						uni.reLaunch({
							url:"/pages/user/index"
						})
						
					}else{
						uni.showToast({ title: '授权失败', icon: 'error', duration: 3000 });
					}
					uni.hideLoading();
					this.ifAuthorize = false
				}
	        }
	    }
</script>

<style lang="scss">
    .header1 {
        padding: 8px 0;
        line-height: 33px;
        border-bottom: 0.5px solid #eee;
        text-align: center;
    }
    .btn {
        background-color: #04be01 !important;
        color: #fff !important;
        border-radius: 44px;
        width: 275px;
        margin-bottom: 20px;
    }
</style>
